#-*- coding: utf-8 -*-
from iFriends.Log.models import RequestEvent, ViewEvent, ResponseEvent, ExceptionEvent
from datetime import datetime



class CustomRequestLogger(object):
	def process_request(self, request):
		try:
			if not request.META['PATH_INFO'].startswith('/admin'):
				event = RequestEvent()
				event.event = "Request"
				event.user = request.user
				event.addr = request.META['REMOTE_ADDR']
				event.date = datetime.now()
				event.url = request.META['PATH_INFO']
				event.save()
		except KeyError:
			return None

class CustomViewLogger(object):
	def process_view(self, request, view, args, kwargs):
		try:
			if not request.META['PATH_INFO'].startswith('/admin'):
				event = ViewEvent()
				event.event = "View"
				event.user = request.user
				event.addr = request.META['REMOTE_ADDR']
				event.date = datetime.now()
				event.view = view.__name__
				event.args = str(args)
				event.kwargs = str(kwargs)
				event.save()
		except KeyError:
			pass
		return None
		
		

class CustomResponseLogger(object):
	def process_response(self, request, response):
		try:
			if not request.META['PATH_INFO'].startswith('/admin'):
				event = ResponseEvent()
				event.event = "Response"
				event.user = request.user
				event.addr = request.META['REMOTE_ADDR']
				event.date = datetime.now()
				event.url = request.META['PATH_INFO']
				event.size = len(response.content)
				event.save()
		except KeyError:
			pass
		return None

class CustomExceptionLogger(object):
	def process_response(self, request, exception):
		try:
			if not request.META['PATH_INFO'].startswith('/admin'):
				event = ExceptionEvent()
				event.event = "Exception"
				event.user = request.user
				event.addr = request.META['REMOTE_ADDR']
				event.date = datetime.now()
				event.url = request.META['PATH_INFO']
				event.exception = exception
				event.save()
		except KeyError:
			pass
		return None
